#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,m;
		scanf("%d %d",&n,&m);
		if(n%2)
		{
			if(n==1) printf("0\n");
			else printf("%d\n",2*m);
		}
		else 
		{
			if(n==2) printf("%d\n",m);
			else printf("%d\n",2*m);
		}
	}
} 
